package org.lanqiao.tjut.control;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lanqiao.tjut.bean.AdminBean;
import org.lanqiao.tjut.bean.OpeBean;
import org.lanqiao.tjut.bean.UserBean;
import org.lanqiao.tjut.myUtils.MD5Util;
import org.lanqiao.tjut.myUtils.MyUtils;
import org.lanqiao.tjut.seriver.AdminService;
import org.lanqiao.tjut.seriver.OpeService;
import org.lanqiao.tjut.seriver.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserInfoControl {
	@Resource
	private AdminService adminService;
	@Resource
	private OpeService OpeService;
	@Resource
	private UserService UserService;

	// 主页登录处理
	@RequestMapping(value = "/home.do")
	public String doLogin(Model model) {
		return "commons/login";
	}

	// 菜单界面
	@RequestMapping(value = "/logined.do")
	public String Logined(String txtAccount, String txtPsw, Model model, HttpSession session,
			HttpServletRequest request, HttpServletResponse response) {

		// 验证身份--管理员
		if (request.getParameter("selectId").equals("管理员")) {
			AdminBean adminB = new AdminBean();
			adminB.setAdminAccount(txtAccount);
			// 在数据库中查询用户登录信息
			AdminBean adminB2 = adminService.selectByAccount(adminB.getAdminAccount());
			if (adminB2 == null) {
				// 设置缓存，在前台页面提示用户名或者密码输入错误
				session.setAttribute("ErroLog", "用户名不存在！");
				// 还回到本页面
				return "commons/login";
				// 验证密码是否正确
			} else if (!txtPsw.equals(adminB2.getAdminPsw())) {
				// 设置缓存，在前台页面提示用户名或者密码输入错误
				session.setAttribute("ErroLog", "用户名或者密码输入错误！");
				// 还回到本页面
				return "commons/login";
			} else {
				// 登录成功，则跳转下一个页面
				session.setAttribute("UserNameA", adminB2);
				return "commons/menuA";
			}
		}
		// 验证身份---操作员
		else if (request.getParameter("selectId").equals("操作员")) {
			OpeBean opeB = new OpeBean();
			opeB.setOpreatorAccount(txtAccount);
			// 在数据库中查询用户登录信息
			OpeBean opeB2 = OpeService.selectByOpeAccount(opeB.getOpreatorAccount());
			if (opeB2 == null) {
				// 设置缓存，在前台页面提示用户名或者密码输入错误
				session.setAttribute("ErroLog", "用户名不存在！");
				// 还回到本页面
				return "commons/login";
				// 验证密码是否正确
			} else if (!MD5Util.MD5(txtPsw).equals(opeB2.getOpreatorPsw())) {
				// 设置缓存，在前台页面提示用户名或者密码输入错误
				session.setAttribute("ErroLog", "用户名或者密码输入错误！");
				// 还回到本页面
				return "commons/login";
			} else {
				// 登录成功，则跳转下一个页面
				session.setAttribute("UserNameO", opeB2);
				return "commons/menuO";
			}
		}
		// 验证身份--用户
		if (request.getParameter("selectId").equals("用户")) {
			UserBean userB = new UserBean();
			userB.setUserAccount(txtAccount);
			// 在数据库中查询用户登录信息
			UserBean userB2 = UserService.selectByUserAccount(userB.getUserAccount());
			if (userB2 == null) {
				// 设置缓存，在前台页面提示用户名或者密码输入错误
				session.setAttribute("ErroLog", "用户名不存在！");
				// 还回到本页面
				return "commons/login";
				// 验证密码是否正确
			} else if (!MD5Util.MD5(txtPsw).equals(userB2.getUserPsw())) {
				// 设置缓存，在前台页面提示用户名或者密码输入错误
				session.setAttribute("ErroLog", "用户名或者密码输入错误！");
				// 还回到本页面
				return "commons/login";
			} else {
				// 登录成功，则跳转下一个页面
				session.setAttribute("UserNameU", userB2);
				return "commons/menuU";
			}
		}
		return null;

	}

	// **********用户信息管理****************************************************************//
	// 显示添加操作员信息
	@RequestMapping(value = "/insertUserInfo.do")
	public String insertUserInfo0() {
		return "userInfoManager/insertUserInfo";
	}

	// 添加用户信息
	@RequestMapping(value = "/Userdetails.do")
	public String insertUserInfo1(UserBean userB, HttpSession session, Model model) {
		if (userB.getUserIndex() == null) {
			String md5psw = MD5Util.MD5(userB.getUserPsw());
			userB.setUserPsw(md5psw);
			// 添加用户
			int i = UserService.insertUserInfo(userB);
			if (i > 0) {
				List<UserBean> lstUser = UserService.selectAllUsers();
				if (lstUser != null) {
					session.setAttribute("lstUser", lstUser);
					return "userInfoManager/UserInfoDetails";
				} else {
					session.setAttribute("Prompt", "暂时没有操作员信息");
					return "opeInfoManager/OpeInfoDetails";
				}
			} else {
				session.setAttribute("Prompt", "添加用户失败！");
				return "opeInfoManager/OpeInfoDetails";
			}
		}
		// 修改用户
		// 修改信息时如果修改密码，则将新密码设置MD5加密，如果不修改则不用设置。
		// MD5密码长度统一为32位
		if (userB.getUserPsw().length() < 30) {
			String md5psw = MD5Util.MD5(userB.getUserPsw());
			userB.setUserPsw(md5psw);
		}
		int i = UserService.updateUserByIndex(userB);
		if (i > 0) {
			List<UserBean> lstUser = UserService.selectAllUsers();
			if (lstUser != null) {
				session.setAttribute("lstUser", lstUser);
				return "userInfoManager/UserInfoDetails";
			} else {
				session.setAttribute("Prompt", "暂时没有操作员信息");
				return "userInfoManager/UserInfoDetails";
			}
		} else {
			session.setAttribute("Prompt", "修改用户失败！");
			return "userInfoManager/UserInfoDetails";
		}
	}

	// 修改用户信息
	@RequestMapping(value = "/updateUserInfo.do")
	public String updateUserInfo(UserBean userB, HttpServletRequest request, HttpServletResponse response) {
		String userIndex = request.getParameter("ope");
		userB = UserService.selectByUserIndex(userIndex);
		request.setAttribute("userB", userB);
		String opt = request.getParameter("opt");
		if (opt.equals("0")) {
			return "userInfoManager/updateUserSelf";
		}
		return "userInfoManager/insertUserInfo";
	}

	// 删除用户信息
	@RequestMapping(value = "/deleteUserInfo.do")
	public String deleteUserInfo(UserBean userB, HttpServletRequest request, HttpServletResponse response,
			HttpSession session) {
		String UserIndex = request.getParameter("ope");
		userB.setUserIndex((MyUtils.obj2Integer(UserIndex)));
		UserService.deleteByUserIndex(userB);
		List<UserBean> lstUser = UserService.selectAllUsers();
		if (lstUser != null) {
			// 删除一个信息后，重新查询所有用户信息并显示
			return selectAllUserInfo(session);
		} else {
			request.setAttribute("Prompt", "暂时没有用户信息");
			return "userInfoManager/UserInfoDetails";
		}
	}

	// 查询所有用户信息
	@RequestMapping(value = "/selectUser.do")
	public String selectAllUserInfo(HttpSession session) {
		// 进行业务逻辑
		List<UserBean> lstUser = UserService.selectAllUsers();
		if (lstUser != null) {
			session.setAttribute("lstUser", lstUser);
			return "userInfoManager/UserInfoDetails";
		} else {
			session.setAttribute("Prompt", "暂时没有用户信息");
			return "userInfoManager/UserInfoDetails";
		}
	}

	// 动态查询操作员信息
	@RequestMapping(value = "/selectUserDynamic.do")
	public String selectUserDynamic0(UserBean userB) {
		return "userInfoManager/selectUserDynamic";
	}

	// 动态查询操作员信息
	@RequestMapping(value = "/UserDynamicdetails.do")
	public String selectUserDynamic1(UserBean userB, HttpSession session, HttpServletRequest request,
			HttpServletResponse response) {
		List<UserBean> lstUser = UserService.selectUserInfoDynamic(userB);
		if (lstUser != null) {
			session.setAttribute("lstUser", lstUser);
			return "userInfoManager/UserInfoDetails";
		} else {
			session.setAttribute("Prompt", "暂时没有用户信息");
			return "userInfoManager/UserInfoDetails";
		}
	}
	// **************************************************************************//

	// 查询所有操作员信息
	@RequestMapping(value = "/selectOpe.do")
	public String selectAllOpeInfo(HttpSession session) {
		// 进行业务逻辑
		List<OpeBean> lstOpe = OpeService.selectAllOpes();
		if (lstOpe != null) {
			session.setAttribute("lstOpe", lstOpe);
			return "opeInfoManager/OpeInfoDetails";
		} else {
			session.setAttribute("Prompt", "暂时没有操作员信息");
			return "opeInfoManager/OpeInfoDetails";
		}
	}

	// 动态查询操作员信息
	@RequestMapping(value = "/selectOpeDynamic.do")
	public String selectOpeDynamic0(OpeBean opeB) {
		return "opeInfoManager/selectOpeDynamic";
	}

	// 动态查询操作员信息
	@RequestMapping(value = "/OpeDynamicdetails.do")
	public String selectOpeDynamic1(OpeBean opeB, HttpSession session, HttpServletRequest request,
			HttpServletResponse response) {
		List<OpeBean> lstOpe = OpeService.selectOpeInfoDynamic(opeB);
		if (lstOpe != null) {
			session.setAttribute("lstOpe", lstOpe);
			return "opeInfoManager/OpeInfoDetails";
		} else {
			session.setAttribute("Prompt", "暂时没有操作员信息");
			return "opeInfoManager/OpeInfoDetails";
		}
	}

	// 显示添加操作员信息
	@RequestMapping(value = "/insertOpe.do")
	public String insertOpeInfo0() {
		return "opeInfoManager/insertOpeInfo";
	}

	// 添加操作员信息
	@RequestMapping(value = "/Opedetails.do")
	public String insertOpeInfo1(OpeBean opeB, HttpSession session, Model model) {
		if (opeB.getOpreatorIndex() == null) {
			// 添加操作员
			String md5psw = MD5Util.MD5(opeB.getOpreatorPsw());
			opeB.setOpreatorPsw(md5psw);
			int i = OpeService.insertOpeInfo(opeB);
			if (i > 0) {
				List<OpeBean> lstOpe = OpeService.selectAllOpes();
				if (lstOpe != null) {
					session.setAttribute("lstOpe", lstOpe);
					return "opeInfoManager/OpeInfoDetails";
				} else {
					session.setAttribute("Prompt", "暂时没有操作员信息");
					return "opeInfoManager/OpeInfoDetails";
				}
			} else {
				session.setAttribute("Prompt", "添加用户失败！");
				return "opeInfoManager/OpeInfoDetails";
			}
		}
		// 修改操作员
		// 修改信息时如果修改密码，则将新密码设置MD5加密，如果不修改则不用设置。
		// MD5密码长度统一为32位
		if (opeB.getOpreatorPsw().length() < 30) {
			String md5psw = MD5Util.MD5(opeB.getOpreatorPsw());
			opeB.setOpreatorPsw(md5psw);
		}
		int i = OpeService.updateOpeByIndex(opeB);
		if (i > 0) {
			List<OpeBean> lstOpe = OpeService.selectAllOpes();
			if (lstOpe != null) {
				session.setAttribute("lstOpe", lstOpe);
				return "opeInfoManager/OpeInfoDetails";
			} else {
				session.setAttribute("Prompt", "暂时没有操作员信息");
				return "opeInfoManager/OpeInfoDetails";
			}
		} else {
			session.setAttribute("Prompt", "修改用户失败！");
			return "opeInfoManager/OpeInfoDetails";
		}
	}

	// 修改操作员信息
	@RequestMapping(value = "/updateOpeInfo.do")
	public String updateOpeInfo(OpeBean opeB, HttpServletRequest request, HttpServletResponse response) {
		String opeIndex = request.getParameter("ope");
		opeB = OpeService.selectByOpeIndex(opeIndex);
		request.setAttribute("opeB", opeB);
		String opt = request.getParameter("opt");
		if (opt.equals("1")) {
			return "opeInfoManager/updateOpeSelf";
		}
		return "opeInfoManager/insertOpeInfo";
	}

	// 删除操作员信息
	@RequestMapping(value = "/deleteOpeInfo.do")
	public String deleteOpeInfo(OpeBean opeB, HttpServletRequest request, HttpServletResponse response,
			HttpSession session) {
		String opeIndex = request.getParameter("ope");
		opeB.setOpreatorIndex(MyUtils.obj2Integer(opeIndex));
		OpeService.deleteByOpeIndex(opeB);
		List<OpeBean> lstOpe = OpeService.selectAllOpes();
		if (lstOpe != null) {
			// 删除一个信息后，重新查询所有操作员信息并显示
			return selectAllOpeInfo(session);
		} else {
			request.setAttribute("Prompt", "暂时没有操作员信息");
			return "opeInfoManager/OpeInfoDetails";
		}
	}

	// 操作员个人信息查询
	@RequestMapping(value = "/queryOpeSelfInfo.do")
	public String queryOpeSelfInfo(OpeBean opeB, HttpServletRequest request) {
		String opeIndex = request.getParameter("index");
		opeB = OpeService.selectByOpeIndex(opeIndex);
		request.setAttribute("opeB", opeB);
		return "opeInfoManager/queryOpeSelf";
	}

	// 操作员修改自己信息
	// 添加操作员信息
	@RequestMapping(value = "/updateOpeSelf.do")
	public String updateOpeSelf(OpeBean opeB, HttpSession session, HttpServletRequest request) {
		// 修改信息时如果修改密码，则将新密码设置MD5加密，如果不修改则不用设置。
		// MD5密码长度统一为32位
		if (opeB.getOpreatorPsw().length() < 30) {
			String md5psw = MD5Util.MD5(opeB.getOpreatorPsw());
			opeB.setOpreatorPsw(md5psw);
		}
		// 修改操作员
		int i = OpeService.updateOpeByIndex(opeB);
		if (i > 0) {
			OpeBean opeB2 = new OpeBean();
			String opeIndex = request.getParameter("opreatorIndex");
			opeB2 = OpeService.selectByOpeIndex(opeIndex);
			request.setAttribute("opeB", opeB2);
			return "opeInfoManager/queryOpeSelf";
		} else {
			session.setAttribute("Prompt", "修改用户失败！");
			return "opeInfoManager/OpeInfoDetails";
		}
	}

	// 用户个人信息查询
	@RequestMapping(value = "/queryUserSelfInfo.do")
	public String queryUserSelfInfo(UserBean userB, HttpServletRequest request) {
		String userIndex = request.getParameter("index");
		userB = UserService.selectByUserIndex(userIndex);
		request.setAttribute("userB", userB);
		return "userInfoManager/queryUserSelf";
	}

	// 用户修改自己信息
	// 添加用户信息
	@RequestMapping(value = "/updateUserSelf.do")
	public String updateUserSelf(UserBean userB, HttpSession session, HttpServletRequest request) {
		// 修改信息时如果修改密码，则将新密码设置MD5加密，如果不修改则不用设置。
		// MD5密码长度统一为32位
		if (userB.getUserPsw().length() < 30) {
			String md5psw = MD5Util.MD5(userB.getUserPsw());
			userB.setUserPsw(md5psw);
		}
		// 修改操作员
		int i = UserService.updateUserByIndex(userB);
		if (i > 0) {
			UserBean userB2 = new UserBean();
			String userIndex = request.getParameter("userIndex");
			userB2 = UserService.selectByUserIndex(userIndex);
			request.setAttribute("userB", userB2);
			return "userInfoManager/queryUserSelf";
		} else {
			session.setAttribute("Prompt", "修改失败！");
			return "userInfoManager/UserInfoDetails";
		}
	}
}
